// pages/mine/renZheng/renZheng.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    idCard: '',
    mobile:'',
    idImageOne:'/images/idCard2.png',
    idImageTwo:'/images/idCard1.png',
    idx:0,
    wechat:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  nameInput(e){
    this.data.name = e.detail.value;

  },
  idCardInput(e) {
    this.data.idCard = e.detail.value;
  },
  mobileInput(e){
    this.data.mobile = e.detail.value;
    if (this.data.mobile.length >= 11) {
      this.setData({
        mobile :this.data.mobile.substring(0, 11)
      });
    }
  },
  wechatInput(e){
    this.data.wechat = e.detail.value;
  },
  chosseImages(e) {
    this.data.idx = e.currentTarget.dataset.key;
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempPaths = res.tempFilePaths;
        _this.upLoadimage(tempPaths[0]);     
      },
    })
  },
  upLoadimage(tempPath) {
    var _this = this;
    wx.showLoading({
      title: '上传中...',
    })

    wx.uploadFile({
      url: app.host + app.upLoadImageUrl,
      filePath: tempPath,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success: function (response) {
        wx.hideLoading();
        if (response.statusCode == 200){
          var string = response.data;
          var dict = JSON.parse(string);
          var url = dict.model.fileFullUrl;
          console.log(url);
          if (_this.data.idx == 0){
            _this.setData({
              idImageOne: url
            });
          } else {
            _this.setData({
              idImageTwo: url
            });
          }
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    })
  },
  
  previewImages(e) {
    var index = parseInt(e.currentTarget.dataset.key);
    var url = this.data.imageList[index];
    var that = this;
    wx.previewImage({
      current: url,
      urls: this.data.imageList,
    })
  },
  upDataClick(){
    var _this = this;
    if (this.data.name == ''){
      app.alert("请输入姓名～");
      return;
    }
    if (this.data.mobile == ""){
      app.alert("请输入手机号码～");
      return;
    }
    if (!(/^[1][3,6,4,5,7,8,9][0-9]{9}$/.test(this.data.mobile))) {
      app.alert("手机号码格式错误～");
      return;
    }
    if (this.data.wechat == ""){
      app.alert("请输入微信号～");
      return;
    }
    if (this.data.idCard == '') {
      app.alert("请输入身份证号码～");
      return;
    }
    if (this.data.idImageOne == '/images/idCard2.png') {
      app.alert("请上传身份证照片～");
      return;
    }
    if (this.data.idImageTwo == '/images/idCard1.png') {
      app.alert("请上传身份证照片～");
      return;
    }
    var map = {};
    map.appid = app.APP_ID;
    map.authMobile = this.data.mobile;
    map.cardBack = this.data.idImageTwo;
    map.cardFront = this.data.idImageOne;
    map.cardNum = this.data.idCard;
    map.name = this.data.name;
    map.weixin = this.data.wechat;
    map.id = app.USER_ID();
    map.openId = wx.getStorageSync("OPEN_ID");
    map.unionid = wx.getStorageSync("UNIONID");

    api.getRequestData(app.userRenZhengUrl, map, false, "POST").then(res => {
      if (res.data.errorCode == 0) {
        app.alert("资料已提交～");
        setTimeout(function () {
          wx.navigateBack({});
        }, 1400);

      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }

    }).catch(err => {
      _this.data.canEvaluate = true;
    });

  },
    
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})